
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;
int led = 22;
bool sw_auto = false;
String input_s = "";
void setup() {
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  digitalWrite(led, sw_auto);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}
void led_s () {
  sw_auto = !sw_auto;
  digitalWrite(led, sw_auto);
  SerialBT.println("led stat");
}
void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  while (SerialBT.available()) {
    char c = SerialBT.read();


    input_s += c;
    input_s.trim();
    Serial.println(input_s);
    if (input_s == "a") {
      led_s ();
      SerialBT.println("ok");
      input_s = "";
    }
    else {
      SerialBT.println("error");
      input_s = "";
    }

  }
  delay(20);
}